<?php


namespace Xy\Rbac\Models;


use Xy\Rbac\ModelTools\BaseModel;

class RbacRoleModel extends BaseModel {
    protected $dateFormat = 'Y-m-d H:i:s';
    protected $searchable = [
        'name',
        'created_at',
    ];
    
    //超级用户的角色id
    const SUPER_USER_ROLE_ID = 1;

    const POWER_ID_SEPARATOR = ',';//权限字段用都好相隔
    
    /**
     * status 状态，0：禁用 1：启用
     */
    const STATUS_NO        = 0;
    const STATUS_NO_TITLE  = '禁用';
    const STATUS_YES       = 1;
    const STATUS_YES_TITLE = '启用';
    
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function userRoles() {
        return $this->hasMany(RbacUserRoleModel::class, 'role_id');
    }
    
    /**
     * @param $value
     *
     * @return array
     */
    public function getPowerAttribute($value) {
        $powerList = json_decode($value, true);
        
        return is_array($powerList) ? $powerList : [];
    }
    
    /**
     * power 入库处理
     *
     * @param $val
     */
    public function setPowerAttribute($val) {
        $powerJson = json_encode(array_values((array)$val));
        
        $this->attributes['power'] = $powerJson;
    }
}